package com.humh.reflection;

import java.lang.reflect.Method;

/**
 * @PackageName: com.humh.reflection
 * @ClassName: UserTest
 * @Description:
 * @author: hgx61
 * @date: 2024/6/22 12:35
 */

public class UserTest {

  public static void main(String[] args) {

    test3();
  }

  public static void test3() {

    try {
      Class<?> userClzss = Class.forName("com.humh.reflection.User");

      Object userObj = userClzss.newInstance();

      Method[] methods = userClzss.getMethods();
      for (Method method : methods) {
        if (method.getName().endsWith("Name") || method.getName().endsWith("Id") || method.getName().equals("sum")) {
          System.out.println("MethodName:  " + method.getName());
        }
      }

      Method methodSum = userClzss.getMethod("sum", int.class, int.class);
      int sm = (int) methodSum.invoke(userObj, 3, 6);
      System.out.println(sm);
      Method methodSetName = userClzss.getMethod("setName", String.class);
      methodSetName.invoke(userObj, "hgx61");
      Method methodGetName = userClzss.getMethod("getName");
      System.out.println(methodGetName.invoke(userObj));



    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void test1() {
    User user = new User();
    user.setName("hgx61");
    user.setId(135);

    int id = user.getId();
    String name = user.getName();
    int sm = user.sum(3, 6);
    System.out.println(id + " " + name + " " + sm);
  }

}
